#include "ws2812b_driver.h"
#include "../../BSP/GPIO_BSP/gpio_bsp.h"

static volatile uint32_t* Keys1;
static volatile uint32_t* Keys2;
static volatile uint32_t* Keys3;
static volatile uint32_t* keys4;


/* 外部触摸按键在这里初始化和使用 */
static void WHT_Button_GPIO_Init(void)
{
    WHT_GPIO_BSP.WHT_Set_Clock(PortB, ENABLE);

    WHT_GPIO_BSP.WHT_Set_Mode(PortB, Pin6, Mode_IPD);
    WHT_GPIO_BSP.WHT_Set_Mode(PortB, Pin7, Mode_IPD);
    WHT_GPIO_BSP.WHT_Set_Mode(PortB, Pin8, Mode_IPD);
    WHT_GPIO_BSP.WHT_Set_Mode(PortB, Pin9, Mode_IPD);

    WHT_GPIO_BSP.WHT_Config_Bit_Input(PortB, Pin6, &keys4);
    WHT_GPIO_BSP.WHT_Config_Bit_Input(PortB, Pin7, &Keys3);
    WHT_GPIO_BSP.WHT_Config_Bit_Input(PortB, Pin8, &Keys2);
    WHT_GPIO_BSP.WHT_Config_Bit_Input(PortB, Pin9, &Keys1);
}


void main_ws2812b(void)
{
    static unsigned int RGB_Color[] = {RGB_Passionate_Coral, RGB_Quiet_light_green, RGB_Warm_Peach_Pink, RGB_Mysterious_Violet, RGB_Vitality_yellow_green};
    static WHT_LRGB_t WHT_LRGB;
    uint8_t pos = 0;
    
    WHT_Button_GPIO_Init();
    WHT_WS2812B_Driver.WHT_Init();

    WHT_WS2812B_Driver.WHT_Set_All_LRGB(&WHT_LRGB);
    WHT_WS2812B_Driver.WHT_Start();

    FunctionalState Switch = DISABLE;
    *(uint32_t*)&WHT_LRGB = RGB_Soft_Warm_Yellow;
    for (; ;)
    {
        /* 按键处理 */
        if (*Keys1)
        {
            vTaskDelay(30);
            if (*Keys1)
            {
                *(uint32_t*)&WHT_LRGB = RGB_Color[pos];
                pos = (pos + 1) % (sizeof(RGB_Color) / sizeof(RGB_Color[0]));
                while (*Keys1)
                {
                    vTaskDelay(30);
                }
                if (Switch == ENABLE)
                {
                    WHT_LRGB.L = 250;
                    WHT_WS2812B_Driver.WHT_Set_All_LRGB(&WHT_LRGB);
                    WHT_WS2812B_Driver.WHT_Start();
                }
            }
        }
        else if (*Keys3 || *keys4)
        {
            vTaskDelay(30);
            if (*Keys3 || *keys4)
            {
                if (Switch == DISABLE)
                {
                    Switch = ENABLE;
                    WHT_LRGB.L = 200;
                }
                else
                {
                    Switch = DISABLE;
                    WHT_LRGB.L = 0;
                }
                while (*Keys3 || *keys4)
                {
                    vTaskDelay(30);
                }
                WHT_WS2812B_Driver.WHT_Set_All_LRGB(&WHT_LRGB);
                WHT_WS2812B_Driver.WHT_Start();
            }
        }
        vTaskDelay(30 / portTICK_RATE_MS);
    }
}
